<!--  -->
<template>
  <div class="panel-main">
    <div class="panel-header">
      MR.GAO合码小工具
    </div>
    <div class='panel-content'>
      <el-form :model="saveForm">
        <el-form-item>
          <el-radio-group v-model="projectModel" @change="changeRadio">
            <el-radio label="shili">市立</el-radio>
            <el-radio label="lmyy">利民</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-row>
          <el-col :span="24">
            <el-form-item label="项目地址：">
              <el-input v-model="saveForm.dir" placeholder="项目地址"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="项目名称：">
              <el-input v-model="value" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="版本：">
              <el-input v-model.trim="version" clearable>
                <template #append>
                  <el-button @click="searchVersion">搜索版本</el-button>
                </template>
              </el-input>

            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item>
              <el-button primary @click="clearSelect" :loading="mergeLoading">清空选中</el-button>
              <el-button @click="toMain">target->main</el-button>
              <el-button @click="toTarget">main->target</el-button>
            </el-form-item>
          </el-col>
          <el-col :span=24>
            <el-form-item label="选择工作站">
              <el-checkbox-group v-model="checkList">
                <el-checkbox v-for="item in projectsRes" :label="item.value" :key="item.value"><span
                    :style="{color:value==item.value?'red':''}">{{ item.value }}</span>
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="源分支">
              <el-input v-model.trim="saveForm.source" placeholder="源分支"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="目标分支">
              <el-input v-model.trim="saveForm.target" placeholder="目标分支"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24" align="center">
            <el-button type="primary" @click="merge" :loading="mergeLoading">合并</el-button>
            <el-button primary @click="updateCommonHandle" :loading="mergeLoading">更新common</el-button>
            <el-button primary @click="updateProjectHandle" :loading="mergeLoading">更新main</el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24"><h2>查找本地分支的工作站</h2></el-col>
          <el-col :span="12">
            <el-input v-model.trim="localBranch" placeholder="本地分支"></el-input>
          </el-col>
          <el-col :span="12">
            <el-button @click="searchLocalBranch">搜索</el-button>
          </el-col>
          <el-col :span="24">
            查询结果:{{ searchResult }}
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24"><h2>本地已合main分支</h2></el-col>
          <el-col :span="24">
            <el-button @click="searchMergeBranch">搜索</el-button>
            <el-button @click="mergeCheckList=mergeResult">全选</el-button>
            <el-button @click="mergeCheckList=[]">取消全选</el-button>
            <el-button @click="deleteBranch">删除本地分支</el-button>
          </el-col>
          <el-col :span="24">
            <el-radio-group v-model="mergeList">
              <el-radio v-for="item in projectsRes" :label="item.value" :key="item.value"><span
                  :style="{color:value==item.value?'red':''}">{{ item.value }}</span>
              </el-radio>
            </el-radio-group>
          </el-col>
          <el-col :span="24" v-loading="exitMergeLoading" style="background: #cca1a1">
            <p>已合分支:</p>
            <el-checkbox-group v-model="mergeCheckList">
              <el-checkbox v-for="item in mergeResult" :label="item" :key="item">{{ item }}
              </el-checkbox>
            </el-checkbox-group>
          </el-col>
        </el-row>

      </el-form>

    </div>
  </div>
</template>

<script script setup>
import {ElMessage, ElMessageBox} from 'element-plus'

import {
  mergeRequest,
  updateProject,
  updateCommon,
  findLocalBranch,
  findMergeLocalBranch,
  delLocalBranch,
  findVersionList,
  getBigInt
} from './services';
import {reactive, ref, computed, onMounted} from 'vue'

let projects = reactive([
  {value: "coid"},
  {value: "dst"},
  {value: "hostpages"},
  {value: "iadm"},
  {value: "inpd"},
  {value: "inpi"},
  {value: "mts"},
  {value: "opb"},
  {value: "opd"},
  {value: "opdm"},
  {value: "opn"},
  {value: "phm"},
  {value: "bcs"},
  {value: "mis"},
  {value: "mds"},
  {value: "common"},
  {value: "cs-ui"}
])
let projectsLm = reactive([
  {value: "coid-lmyy"},
  {value: "dst-lmyy"},
  {value: "hostpages-lmyy"},
  {value: "iadm-lmyy"},
  {value: "inpd-lmyy"},
  {value: "inpi-lmyy"},
  {value: "mts-lmyy"},
  {value: "opb-lmyy"},
  {value: "opd-lmyy"},
  {value: "opdm-lmyy"},
  {value: "opn-lmyy"},
  {value: "phm-lmyy"},
  {value: "mis-lmyy"},
  {value: "common_lib_lmyy"},
])
let localBranch = ref('')
let projectModel = ref("shili")
let saveForm = reactive({
  dir: 'D:\\wegao\\gitProject\\HIS\\',
  source: '',
  target: ''
})
let searchResult = ref('')
let checkList = ref([])
let value = ref('')
let version = ref('')
let mergeLoading = ref(false)
let projectsRes = computed(() => {
  if (projectModel.value === 'shili') {
    return projects
  } else {
    return projectsLm
  }
})
let showInfo = ref(false)
let searchVersion = async () => {
  let params = {
    version: version.value
  }
  console.log("checkList", checkList)
  let res = await findVersionList(params)
  if (res.errorNo === 0) {
    let arr = res.list.map(item => item.split("-")[0].toLowerCase())
    checkList.value = [].concat(arr)
  }
  console.log("搜索结果", res, checkList)
}
let searchLocalBranch = async () => {
  let projects = projectsRes.value.map(item => item.value)
  const params = {
    dir: saveForm.dir,
    projects: projects,
    branch: localBranch.value
  }
  try {
    mergeLoading.value = true;
    searchResult.value = ''
    console.log("参数123", params)
    const {errorNo, res} = await findLocalBranch(params);
    searchResult.value = res.filter(item => item.res).map(item => item.project).join(',')
    if (errorNo === 0) {
      ElMessageBox.confirm("查找成功", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'success'
      })
    }
  } finally {
    mergeLoading.value = false;
  }
}
let mergeList = ref('')
let mergeCheckList = ref([])
let mergeResult = ref([])
let exitMergeLoading = ref(false)
let searchMergeBranch = async () => {
  exitMergeLoading.value = true;
  if (!mergeList) {
    ElMessage.warning({message: "请选择工作站"})
    return;
  }
  let projects = [mergeList.value]
  const params = {
    dir: saveForm.dir,
    projects: projects,
    branch: localBranch.value
  }
  try {
    mergeResult.value = []
    console.log("参数", params)
    const {errorNo, res} = await findMergeLocalBranch(params);
    console.log(errorNo, res[0].res)
    // res.forEach(item=>{
    //   console.log("item..",item)
    // })
    let map = res[0].res.filter((item) => item.toString().indexOf('main') === -1 && item.toString()).map((item) => item.toString().trim())
    console.log("map..", map)
    mergeResult.value = map
  } finally {
    exitMergeLoading.value = false;
    mergeLoading.value = false;
  }
}
let deleteBranch = async () => {
  if (mergeCheckList.value.length === 0) {
    ElMessage.warning({message: "请选择分支"})
    return;
  }
  let flag = true;
  flag = await ElMessageBox.confirm("确认删除", '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    dangerouslyUseHTMLString: true,
    type: 'success'
  }).catch(() => {
    flag = false
  })
  if (!flag) {
    return;
  }
  let projects = [mergeList.value]
  let branch = mergeCheckList.value
  const params = {
    dir: saveForm.dir,
    projects: projects,
    branch: branch
  }
  try {
    mergeResult.value = []
    console.log("参数", params)
    const {errorNo, res} = await delLocalBranch(params);
    console.log(errorNo, res[0].res)
    if (errorNo === 0) {
      ElMessage.success({message: "删除成功"})
      searchMergeBranch()
    }
    // res.forEach(item=>{
    //   console.log("item..",item)
    // })
    // let map=res[0].res.filter((item)=>item.toString().indexOf('main')===-1&&item.toString()).map((item)=>item.toString().trim())
    // console.log("map..",map)
    // mergeResult.value=map
  } finally {
    mergeLoading.value = false;
  }
}
let merge = async () => {
  if (!saveForm.dir) {
    ElMessage.warning({message: "项目地址不能为空"})
    return;
  }
  if (checkList.value.length === 0) {
    ElMessage.warning({message: "请选择工作站"})
    return;
  }
  if (!saveForm.target || !saveForm.source) {
    ElMessage.warning({message: "请完善分支信息"})
    return;
  }
  try {
    mergeLoading.value = true;
    const params = {
      dir: saveForm.dir,
      projects: checkList.value,
      source: saveForm.source,
      target: saveForm.target
    }
    console.log("参数", params)
    const {errorNo, success, error, updated} = await mergeRequest(params);
    if (errorNo === 0) {
      // this.$message.success("合并成功")
      let str = `<div>成功：${success.join(",")}</div><div>失败：${error.join(",")}</div><div>无需更新：${updated.join(",")}</div>
<div>信息：</div><div>${checkList.value.join(',')}(${saveForm.source})已合${saveForm.target}</div>`
      ElMessageBox.confirm(str, '提示', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      })

    } else {
      ElMessage.error("合并失败")
    }
  } catch (e) {
    console.log("劫夺", e)
    // this.showMsg('ERROR')
  } finally {
    mergeLoading.value = false
  }

}
let clearSelect = () => {
  checkList.value = []
}
let toMain = () => {
  let params = {...saveForm}
  if (params.target && params.target !== 'main') {
    saveForm.source = params.target
  }
  if (saveForm.source === 'main') {
    saveForm.source = ''
  }
  saveForm.target = 'main'
}
let toTarget = () => {
  let params = {...saveForm}
  console.log("version")
  if (!saveForm.target && version) {
    console.log(saveForm, version.value)
    saveForm.target = version.value
  }
  if (params.source && params.source !== 'main') {
    saveForm.target = params.source
  }
  if (saveForm.target === 'main') {
    saveForm.target = ''
  }
  saveForm.source = 'main'
}
let changeRadio = (val) => {
  clearSelect()
  if (val === 'shili') {
    saveForm.dir = 'D:\\wegao\\HIS\\'
  } else {
    saveForm.dir = 'D:\\wegao\\LMHIS\\'
  }
}
let updateCommonHandle = async () => {
  const params = {
    dir: saveForm.dir,
    projects: checkList.value,
    source: saveForm.source,
    target: saveForm.target
  }
  try {
    mergeLoading.value = true;
    console.log("参数", params)
    const {errorNo, success, error, updated} = await updateCommon(params);
    if (errorNo === 0) {
      ElMessageBox.confirm("更新成功", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'success'
      })
    }
  } finally {
    mergeLoading.value = false;
  }
}
let updateProjectHandle = async () => {
  const params = {
    dir: saveForm.dir,
    projects: checkList.value,
    source: saveForm.source,
    target: saveForm.target
  }
  try {
    mergeLoading.value = true;
    console.log("参数", params)
    const {errorNo, success, error, updated} = await updateProject(params);
    if (errorNo === 0) {
      ElMessageBox.confirm("更新成功", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'success'
      })
    }
  } finally {
    mergeLoading.value = false;
  }

}
let getBigIntData = async () => {
  let res = await getBigInt()
  console.log('res', res)
}
onMounted(() => {
  console.log("初始化", projectsRes)
  getBigIntData()
})

</script>
<style lang='less' scoped>
.panel-main {
  height: 100vh;
  width: 100%;
  background: rgba(185, 182, 182, 0.26);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;

  .panel-header {
    background: #00ad50;
    width: 800px;
    padding: 12px;
    font-size: 24px;
    text-align: center;
    color: #fff;
  }

  .panel-content {
    width: 800px;
    padding: 12px;
    background: #d8e4ef;
  }

  .box1 {
    background: url("https://www.google.com/")
  }

  .box2 {
    background: url("https://www.baidu.com/")
  }
}
</style>